What is del?
The del npm package is a powerful tool for deleting files and directories in a Node.js environment. It uses globs to select files and is built on top of the rimraf package, providing a promise-based API that supports multiple files and globbing patterns.
What are del's main functionalities?
Delete files and directories
This feature allows you to delete files and directories using glob patterns. The example shows how to delete all JavaScript files in the 'temp' directory except 'temp/unicorn.js'.
const del = require('del');
del(['temp/*.js', '!temp/unicorn.js']).then(paths => {
console.log('Deleted files and folders:\n', paths.join('\n'));
});
Dry run
This feature performs a dry run without actually deleting the files. It's useful for testing which files would be deleted.
const del = require('del');
del(['temp/*.js'], {dryRun: true}).then(paths => {
console.log('Files and folders that would be deleted:\n', paths.join('\n'));
});
Force deletion
This feature allows you to delete files outside of the current working directory. Use with caution as it can potentially cause data loss.
const del = require('del');
del(['../temp/*.js'], {force: true}).then(paths => {
console.log('Deleted files and folders:\n', paths.join('\n'));
});
Other packages similar to del
rimraf
rimraf is a Node.js package that provides a UNIX command rm -rf like functionality. It is the underlying library that del uses to delete files and directories. Unlike del, rimraf does not return promises natively and does not support globbing patterns without additional modules.
fs-extra
fs-extra is a package that extends the built-in Node.js fs module. It includes methods like remove and emptyDir which can be used to delete files and directories. fs-extra supports promises and can be a more comprehensive file system solution, but it does not have built-in globbing support.
globby
globby is a globbing library for Node.js that can be used in conjunction with other file system packages to delete files using patterns. It is not a direct alternative to del, but it can be used to achieve similar results when combined with fs or fs-extra.
del
Delete files and directories using globs
Similar to rimraf, but with a Promise API and support for multiple files and globbing. It also protects you against deleting the current working directory and above.
Install
$ npm install del
Usage
const del = require('del');
(async () => {
const deletedFilePaths = await del(['temp/*.js', '!temp/unicorn.js']);
const deletedDirectoryPaths = await del(['temp', 'public']);
console.log('Deleted files:\n', deletedFilePaths.join('\n'));
console.log('\n\n');
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));
})();
Beware
The glob pattern **
matches all children and the parent.
So this won't work:
del.sync(['public/assets/**', '!public/assets/goat.png']);
You have to explicitly ignore the parent directories too:
del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
To delete all subdirectories inside public/
, you can do:
del.sync(['public/*/']);
Suggestions on how to improve this welcome!
API
Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use path.posix.join()
instead of path.join()
.
del(patterns, options?)
Returns Promise<string[]>
with the deleted paths.
del.sync(patterns, options?)
Returns string[]
with the deleted paths.
patterns
Type: string | string[]
See the supported glob patterns.
options
Type: object
You can specify any of the globby
options in addition to the below options. In contrast to the globby
defaults, expandDirectories
, onlyFiles
, and followSymbolicLinks
are false
by default.
force
Type: boolean
Default: false
Allow deleting the current working directory and outside.
dryRun
Type: boolean
Default: false
See what would be deleted.
const del = require('del');
(async () => {
const deletedPaths = await del(['temp/*.js'], {dryRun: true});
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
})();
concurrency
Type: number
Default: Infinity
Minimum: 1
Concurrency limit.
onProgress
Type: (progress: ProgressData) => void
Called after each file or directory is deleted.
import del from 'del';
await del(patterns, {
onProgress: progress => {
}});
ProgressData
{
totalCount: number,
deletedCount: number,
percent: number
}
percent
is a value between 0
and 1
CLI
See del-cli for a CLI for this module and trash-cli for a safe version that is suitable for running by hand.
del for enterprise
Available as part of the Tidelift Subscription.
The maintainers of del and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.
Related
- make-dir - Make a directory and its parents if needed
- globby - User-friendly glob matching